package com.zy.pro.user.web;

import java.io.PrintWriter;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.system.SystemCnt;
import com.zy.pro.user.model.User;
import com.zy.pro.user.service.UserService;
import com.zy.pro.user.web.form.UserSearchForm;
import com.zy.utils.MethodUtil;
import com.zy.utils.SessionUtils;
/**
 * 系统管理员管理控制器
 *
 */
@Controller
@RequestMapping("/control/user/userController")
public class UserController extends BaseController {

	private static final Logger logger = Logger.getLogger(UserController.class);
	
	/**
	 * 系统管理员管理服务类
	 */
	@Resource(name = "userServiceImpl")
	private UserService userService;
	
	/**
	 * 列出后台管理用户列表页面
	 */
	@RequestMapping("/sysUserList")
	public String sysUserList(UserSearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			if(form.getUserName() != null && form.getUserName() != ""){
				form.setUserName(form.getUserName().trim());
			}
			if(form.getUserPhone() != null && form.getUserPhone() != ""){
				form.setUserPhone(form.getUserPhone().trim());
			}
			form.getOrderby().put("userCreateTime", "desc");
			//创建页面对象
			PageView<User> pageView = new PageView<User>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(userService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			map.addAttribute("reqUrl", "/control/user/userController/sysUserList.do");
			//TODO 转发页面
			return "/control/system/user/sysUserList";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	/**
	 * 编辑用户状态
	 */
	@RequestMapping("/editStatus")
	public void editStatus(UserSearchForm form, ModelMap map,HttpServletRequest request,HttpServletResponse response) {
		try {
			response.setContentType("text/html");
			PrintWriter out;
			out = response.getWriter();
			User u = userService.find(form.getUserId());
			if(null!=u){
				User user = new User();
				user.setUserId(form.getUserId());
				user.setUserStatus(form.getUserStatus());//1:正常,2:冻结,3:删除
				userService.updateBySelective(user);
				out.print("true");
			}else{
				out.print("false");
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}
	
	
	
	/**
	 * 业主登录
	 * @param user
	 * @param map
	 * @param request
	 * @return
	 */
	/*@RequestMapping("/login")
	public void login(User user, ModelMap map,HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		try {
			String message = "";
			if(user != null && user.getUserName() != null && !"".equals(user.getUserName().trim()) && user.getPassword() != null && !"".equals(user.getPassword().trim())){
				user.setPassword(MethodUtil.MD5(user.getPassword()));
				User userInfo = userService.findUserNameAndPassword(user);
				if(userInfo != null && userInfo.getUserStatus() != 2){
					userInfo.setUserOldIP(userInfo.getUserNewIP());
					if(userInfo.getUserNewLoginTime() != null){
						userInfo.setUseOldLoginTime(userInfo.getUserNewLoginTime());
					}
					userInfo.setUserNewIP(SystemCnt.getIpAddr(request));
					userInfo.setUserNewLoginTime(new Date());
					
					userService.updateBySelective(userInfo);
					
					//将店家信息放置到session当中
					SessionUtils.setUser(request, userInfo);
					SessionUtils.setRoleType(request, userInfo.getRoleType());
					response.getWriter().print(userInfo.getUserId().toString()+"success");
				}else{
					if(userInfo == null){
						message = "该用户不存在或密码错误";
					}else if(userInfo.getUserStatus() == 2){
						message = "用户已经被冻结,请联系管理员";
					}
					response.getWriter().print(message);
				}
			}else{
				if(user != null){
					if(user.getUserName() == null || "".equals(user.getUserName().trim())){
						message = "用户名不能为空";
					}else{
						message = "密码不能为空";
					}
				}else{
					message = "用户名和密码不能为空";
				}
				response.getWriter().print(message);
			}
		} catch(Exception e){
			logger.error(e);
		}
	}
	
	
	*//**
	 * 列出系统管理员管理
	 *//*
	@RequestMapping("/list")
	public String list(UserSearchForm form, ModelMap map) {
		try {
			//设置查询排序
			//TODO eg:form.getOrderby().put("id", "asc");
			
			
			//创建页面对象
			PageView<User> pageView = new PageView<User>(12, form.getPage());
			form.setFirstResult(pageView.getFirstResult());
			form.setMaxResult(pageView.getMaxresult());
			pageView.setQueryResult(userService.getData(form));
			map.addAttribute("pageView", pageView);
			map.addAttribute("form", form);
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 显示添加系统管理员管理
	 *//*
	@RequestMapping("/displayAdd")
	public String displayAdd(User user, ModelMap map) {
		try {
			//TODO 加载显示页面数据
			
			
			//TODO 转发页面
			return "";
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 添加系统管理员管理
	 *//*
	@RequestMapping("/add")
	public String add(User user, ModelMap map) {
		try {
			//TODO 设置user  eg:主键信息
			
			userService.save(user);
			return list(new UserSearchForm(), map);
		} catch(Exception e){
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 显示编辑系统管理员管理
	 *//*
	@RequestMapping("/displayEdit")
	public String displayEdit(UserSearchForm form, ModelMap map) {
		try {
			//TODO 查询对象
			//eg:User user = userService.find(form.getId());
			//eg:map.addAttribute("entity", user);
			
			
			//TODO 转发页面
			return "";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 编辑系统管理员管理
	 *//*
	@RequestMapping("/edit")
	public String edit(User User, ModelMap map) {
		try {
			//TODO:处理系统管理员管理
			
			
			userService.updateBySelective(User);
			return list(new UserSearchForm(), map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}

	*//**
	 * 删除单个系统管理员管理
	 *//*
	@RequestMapping("/deleteuser")
	public String delete(UserSearchForm form, ModelMap map) {
		try {
			//TODO  eg:userService.delete(form.getId());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}
	
	*//**
	 * 批量删除系统管理员管理
	 *//*
	@RequestMapping("/batchDelete")
	public String deletes(UserSearchForm form, ModelMap map) {
		try {
//			TODO  eg:userService.batchDelete(form.getIds());
			
			return list(form, map);
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
	}*/
}
